require "Common/define"
require "Logic/Config"
require "Common/functions"

UIFuncMenu = {};
HelperFunc.SetUIEnv(UIFuncMenu)
local this = UIFuncMenu;

this.gameObject = nil;
this.content1 = nil;
this.content2 = nil;
this.tab_1 = {};
this.tab_2 = {};
this.tab_3 = {};
this.tab_func = {};
this.wardPar = nil;
this.state = nil;
this.controller = nil;
this.listController1 = nil;
this.poolPar = nil;
this.itemObjList = {};
this.imgContent1 = nil;
this.imgContent2 = nil;
this.funcList1 = nil;
this.funcList2 = nil;
this.listController1 = nil;
this.listController2 = nil;
this.count1 = nil;
this.count2 = nil;
this.leftNum = 3; -- 左侧边栏不收缩数量
this.rightNum = 3;-- 右侧边栏不收缩数量
this.firstPayIndex = 2;-- 首充位置
this.firstGiftIndex = 3;-- 限时礼包位置
this.timeId = nil
this.timeIdList = {};

-- 活动按钮
this.btnActivity = nil

this.hasInit = false;
this.removeRedLis = false;
this.btnItem = nil;
this.costUpdate = false;

--- 限时活动数据集合
--- @type table<string, any>
local mActServerData = {}

function UIFuncMenu.Init(isState)
    if isState ~= nil then
        state = isState;
    else
        state = 0;
    end
    local name, packageName = getUIName(PanelNames.UIFuncMenu);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIFuncMenu.onCreate(obj)
 
    if not hasInit then
        hasInit = true;
        this.isOpen = 1;
        gameObject = obj;
        controller = obj:GetController("c1");
        controller.selectedIndex = state;
        imgContent1 = obj:GetChild("imgContent2");
        imgContent2 = obj:GetChild("imgContent1");
        content1 = imgContent1:GetChild("list");
        content2 = imgContent2:GetChild("list");
        listController2 = imgContent2:GetController("c1")
        listController1 = imgContent1:GetController("c1")
        btnItem = imgContent1:GetChild("btnItem");
        wardPar = obj:GetChild("ward");
        poolPar = obj:GetChild("pool");
        anim = obj:GetTransition("Effect_JumpOut")

        --右侧功能栏
        funcList1 = obj:GetChild("imgContent2"):GetChild("menuPullBtn");
        funcList1.gameObjectName = "funcList1"
        funcList2 = obj:GetChild("imgContent1"):GetChild("menuPullBtn");
        funcList2.gameObjectName = "funcList2"
        showRedDots1 = funcList1:GetController("isShowRedDots");
        showRedDots1.selectedIndex = 1;
        showRedDots2 = funcList2:GetController("isShowRedDots");
        showRedDots2.selectedIndex = 1;

        -- 活动按钮
        this.btnActivity = obj:GetChild("Btn_Activity")

        this.addEvent();
    end
    this.initData();
    if anim then
        anim:Play(1, 0.06, 0, -1, nil);
    end
end

function UIFuncMenu.addEvent()
    --btn_Close.onClick:Add(this.onClickClose);
    --btn_Back.onClick:Add(this.onClickClose);

    -- 屏蔽左侧列表展开
    funcList1.onClick:Add(function()
        if isOpenUI(PanelNames.UIHeadMenu) and CtrlManager.GetCtrl(PanelNames.UIHeadMenu).isHaveShowMask() then
            return ;
        end
        this.isOpen = nil;
        if listController1.selectedIndex == 0 then
            listController1.selectedIndex = 1
            this.onOpenList(1);
        else
            listController1.selectedIndex = 0
            this.onCloseList(1);
        end
    end);

    funcList2.onClick:Add(function()
        if isOpenUI(PanelNames.UIHeadMenu) and CtrlManager.GetCtrl(PanelNames.UIHeadMenu).isHaveShowMask() then
            return ;
        end
        if listController2.selectedIndex == 0 then
            listController2.selectedIndex = 1;
            this.onOpenList(2);
        else
            listController2.selectedIndex = 0;
            this.onCloseList(2);
        end
    end)

    -- 活动
    this.btnActivity.onClick:Add(this.onClickActivity)

    --listController1.onChanged:Add(function(evt)
    --    --print(evt);
    --    this.isOpen = nil;
    --    local idx = evt.sender.selectedIndex;
    --    if idx == 1 then
    --        this.onOpenList(1);
    --    else
    --        this.onCloseList(1);
    --    end
    --end);
    --listController2.onChanged:Add(function(evt)
    --    --print(evt);
    --    local idx = evt.sender.selectedIndex;
    --    if idx == 1 then
    --        this.onOpenList(2);
    --    else
    --        this.onCloseList(2);
    --    end
    --end);

    ListenEvent(Config.EventType.BottomChangePage, this.onEventBottomChangePage)
end

function UIFuncMenu.setState(isState)
    controller.selectedIndex = isState;
end

function UIFuncMenu.removeEvent()
    --btn_Close.onClick:Remove(this.onClickClose);
    --btn_Back.onClick:Remove(this.onClickClose);

    funcList1.onClick:Clear();
    funcList2.onClick:Clear();
    UIRedDots.removeViewEvent(PanelNames.UIFuncMenu)
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UIFuncMenu);
    CloseEvent(Config.EventType.BottomChangePage, this.onEventBottomChangePage)

    this.itemObjList = {};
end

function UIFuncMenu.LocalListenEvent()
    ListenEvent(Config.EventType.Get_DropAward, this.DropWards);
    ListenEvent(Config.EventType.Fresh_FuncMenuData, this.initData);
    ListenEvent(Config.EventType.Fresh_Active, this.initData);
    ListenEvent(Config.EventType.RankBattleInfo, this.UpdateState)
    ListenEvent(Config.EventType.FlyBagTransition,this.FlyBagTransition)
    ListenEvent(Config.EventType.UICostUpdate,this.costUpdateData);
end

function UIFuncMenu.LocalCloseEvent()
    CloseEvent(Config.EventType.Get_DropAward, this.DropWards);
    CloseEvent(Config.EventType.Fresh_FuncMenuData, this.initData);
    CloseEvent(Config.EventType.Fresh_Active, this.initData);
    CloseEvent(Config.EventType.RankBattleInfo, this.UpdateState)
    CloseEvent(Config.EventType.FlyBagTransition,this.FlyBagTransition)
    CloseEvent(Config.EventType.UICostUpdate,this.costUpdateData);
end

function UIFuncMenu.costUpdateData()
    local Table_FunctionMenu = G.dataTable["t_functionmenu"];
    local cfg = Table_FunctionMenu["10"];
    if cfg == nil then
        return;
    end
    local severData = logicMgr.CostManager.getCostData(cfg.f_JumpCo);
    if severData == nil or severData.hasGetAllReward == nil or (not FuncOpenManager.isOpen(cfg.f_FuncOpen, false)) then
        return;
    end
    if (severData.hasGetAllReward and this.costUpdate) or (not severData.hasGetAllReward and not this.costUpdate) then
        this.initData();
    end
end

function UIFuncMenu.OnDestroy()
    mActServerData = nil
    hasInit = false
    for k, v in pairs(this.timeIdList) do
        utimer.remove(v);
    end
    this.timeIdList = {};
    this.removeEvent();
    HelperFunc.ClearFlyList()
    this.RemoveTime()
end

function UIFuncMenu.OnEnable()
    this.LocalListenEvent();
end

function UIFuncMenu.OnDisable()
    this.LocalCloseEvent();
    for k, v in pairs(this.timeIdList) do
        utimer.remove(v)
    end
    this.timeIdList = {}
    if this.removeRedLis then
        UIRedDots.removeViewEvent(PanelNames.UIFuncMenu)
    end
end

function UIFuncMenu.DropWards(msg)
    if content2.numChildren > 3 then

        if table.getn(msg["itemList"]) > 0 then
            --listController1.selectedIndex = 1;
            local target = content2:GetChildAt(2);
            local trans = target:GetTransition("t0")
            HelperFunc.FlyToBagByItemData(msg["itemList"], wardPar, poolPar,target,trans)
            --[[local w = Screen.width
            local h = Screen.height
            if w < 1080 then
                w = 1080
            end
            if h < 1920 then
                h = 1920
            end
            local startPos = gameObject:TransformPoint(Vector2(w / 2, h / 3), wardPar)
            HelperFunc.FlyToBagByItemData(msg["itemList"], wardPar, poolPar, startPos, content2:TransformPoint(Vector2(targetpos.x, targetpos.y), wardPar), trans);]]
        end
    end
end

function UIFuncMenu.initData()
    local Table_FunctionMenu = G.dataTable["t_functionmenu"];
    tab_func = {};
    for k, v in pairs(Table_FunctionMenu) do
        table.insert(tab_func, v);
    end

    table.sort(tab_func, function(a, b)
        return a.f_Sort < b.f_Sort;
    end)

    tab_1 = {};
    tab_2 = {};
    tab_3 = {};
    this.removeRedLis = false;
    this.costUpdate = true;
    for k, v in pairs(this.timeIdList) do
        utimer.remove(v);
    end
    this.timeIdList = {};
    UIRedDots.removeViewEvent(PanelNames.UIFuncMenu)
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UIFuncMenu);
    this.itemObjList = {};
    local isHaveFirstGift = this.firstGiftIndex;
    local limitFuncList = {} -- 限时活动列表（f_Group为1）
    for i = 1, #tab_func do
        local v = tab_func[i];
        local funcType = tonumber(v.f_FuntionType);
        if funcType == EnumConst.FuncMenuTypeEnum.FUNCTION then
            local flag = true
            if v.f_ID == 7 and not logicMgr.RankBattleManager.isRankBattleOpen() then
                flag = false
            end
            if v.f_ID == 8 and (not logicMgr.NoticeManager.IsOpen() or logicMgr.NoticeManager.IsTimeEnd()) then --公告牌
                flag = false
            end
            if v.f_ID == 10 then
                local severData = logicMgr.CostManager.getCostData(v.f_JumpCo);
                if severData == nil or severData.hasGetAllReward == nil or severData.hasGetAllReward then
                    flag = false
                    this.costUpdate = false;
                end
            end
            if v.f_ID == 11 then
                flag = false;
                --if logicMgr.NoticeManager.getSevenRankDay() == nil then
                --    flag = false
                --else
                --    isHaveFirstGift = isHaveFirstGift + 1;
                --end
            end
            if v.f_ID == 12 then
                flag = false
                local unionWarState, unionWarStateEndTime = logicMgr.GuildBattlePlusManager.getOpenState(EnumConst.GuildBattlePlusType.Championship);
                if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.GuildBattle) and unionWarState == EnumConst.GuildBattlePlusState.Open and logicMgr.GuildBattlePlusManager.getQualification(EnumConst.GuildBattlePlusType.Championship) == 1 then
                    flag = true
                    if logicMgr.GuildBattlePlusManager.getBattleType() == 1 then
                        v.f_Desc = "CommonPop_Title81";
                    else
                        v.f_Desc = "CommonPop_Title80";
                    end
                end
            end
            if v.f_ID == 13 then
                if logicMgr.NoticeManager.getSevenRankPlusDay() == nil then
                    flag = false;
                else
                    isHaveFirstGift = isHaveFirstGift + 1;
                end
            end
            if flag then
                if FuncOpenManager.isOpen(v.f_FuncOpen, false) then
                    -- table.insert(tab_1, v)
                    if v.f_Group == 1 --[[限时活动单独处理]]then
                        table.insert(limitFuncList, v)
                    else
                        table.insert(tab_1, v)
                    end
                else
                    if v.f_ID == 1 then
                        this.removeRedLis = true;
                    end
                end
            end
        elseif funcType == EnumConst.FuncMenuTypeEnum.ACTIVITY then
            if v.f_ID == 15 then
                if Config.RemoteConfig.TapOpen ~= nil and  Config.RemoteConfig.TapOpen == 1 and FuncOpenManager.isOpen(v.f_FuncOpen, false) then
                    table.insert(tab_2, v);
                end
            else
                if FuncOpenManager.isOpen(v.f_FuncOpen, false) then
                    table.insert(tab_2, v);
                end
            end
        elseif funcType == EnumConst.FuncMenuTypeEnum.TEST then
            table.insert(tab_3, v);
        end
    end
    local activeList = logicMgr.ActiveManager.getActiveEntranceDataListByType(EnumConst.ActiveShowType.LeftSidebar);
    for i = 1, #activeList do
        local v = activeList[i];
        if FuncOpenManager.isOpen(v.f_FuncOpen, false) then
            table.insert(tab_1, v);
        end
    end

    local costData = logicMgr.CostManager.getFirstPayData(logicMgr.CostManager._FIRSTPAYGIFTID);
    costData.f_HintType=Config.RedDotsType.FirstPayGift;
    local firstPayState=(logicMgr.CostManager.getFunMenuById(logicMgr.CostManager._FIRSTPAYGIFTID)~=nil)
    local accumulateState=logicMgr.ActiveManager.getActiveData(logicMgr.CostManager._ACCUMULATEPAYID);
    local isAccumulatePayOpen= accumulateState ~= nil and accumulateState~=3 and accumulateState~=-1
    if firstPayState or isAccumulatePayOpen then
        table.insert(tab_1, math.min(#tab_1 + 1, this.firstPayIndex), costData);
        isHaveFirstGift = isHaveFirstGift + 1
    end

    -- 返利标签特殊写
    local voucherData = logicMgr.CostManager.GetVoucherData();
    if logicMgr.CostManager.GetServerRebateType() == 1 or (logicMgr.CostManager.HasVoucherReward() and voucherData ~= nil and voucherData.rewarded == 0) then
        if FuncOpenManager.isOpen(706, false) then
            local flag = {};
            flag.f_ID = 10001
            flag.f_FuntionType = 1;
            flag.f_Icon = "home_right_icon_fanli";
            flag.f_OpenUI = "UICoupon";
            flag.f_Desc = "FunctionMenu_Desc18";
            flag.f_HintType = Config.RedDotsType.VoucherReward;
            flag.f_FuncOpen = "";
            table.insert(tab_1, math.min(#tab_1 + 1, isHaveFirstGift), flag);
            isHaveFirstGift = isHaveFirstGift + 1;
        end
    end

    local flashGiftList = logicMgr.FlashGiftManager.getShowFunFlashGiftList();
    for i = 1, #flashGiftList do
        local v = flashGiftList[i];
        if FuncOpenManager.isOpen(v.f_FuncOpen, false) then
            table.insert(tab_1, math.min(#tab_1 + 1, isHaveFirstGift), v);
        end
        if isHaveFirstGift <= this.leftNum then
            this.isOpen = nil;
        end
        if this.isOpen == 1 then
            this.isOpen = 2;
        end
        break ;
    end

    -- 限时活动，入口单独展示（f_Group为1）
    local data = this.getLatestLimitActData(limitFuncList)
    if data ~= nil then
        table.insert(tab_1, data)
    end

    local count = 0;
    for k, v in pairs(tab_1) do
        if v.f_HintType ~= nil and v.f_HintType > 0  then
            count = count + 1;
            if count > this.leftNum then
                if (v.f_OpenUI == "UICost" and v.f_JumpCo ~= "") or v.f_Group == 1 then
                    UIRedDots.addViewListen(PanelNames.UIFuncMenu, gameObject.gameObjectName .. funcList1.gameObjectName, funcList1, v.f_HintType, tonumber(v.f_JumpCo));
                else
                    UIRedDots.addViewListen(PanelNames.UIFuncMenu, gameObject.gameObjectName .. funcList1.gameObjectName, funcList1, v.f_HintType, v.id);
                end
            end
        end
    end
    count = 0;
    for k, v in pairs(tab_2) do
        if v.f_HintType ~= nil and v.f_HintType > 0 then
            count = count + 1;
            if count > this.rightNum then
                UIRedDots.addViewListen(PanelNames.UIFuncMenu, gameObject.gameObjectName .. funcList2.gameObjectName, funcList2, v.f_HintType, v.id);
            end
        end
    end

    count1 = tab_1;
    count2 = tab_2;
    local count3 = #tab_3;
    this.UpdateState()
    content2.itemRenderer = this.RenderListItem2;
    content2.numItems = #tab_2;
    if #tab_2 == 0 then
        imgContent2.visible = false;
    else
        imgContent2.visible = true;
        this.onCloseList(2);
        funcList2.visible = this.getListNum(2) > this.rightNum;
    end

    --content3.itemRenderer = this.RenderListItem3;
    --content3.numItems = count3;
    this.RemoveTime()
    this.delayTimer = utimer.delayCall(this.DelaySetBagPos,500)

    -- 活动红点
    UIRedDots.addViewListen(PanelNames.UIFuncMenu, "UIFuncMenu" .. this.btnActivity.name, this.btnActivity, Config.RedDotsType.ActivityCentre)
end
function UIFuncMenu.RemoveTime()
    utimer.remove(this.delayTimer)
    this.delayTimer = nil
end
function UIFuncMenu.DelaySetBagPos()
    --Vector3(0,2*140,0)
    if not hasInit then return end
    if content2 and content2.numChildren > 2 then
        local bag = content2:GetChildAt(2)
        local pos = Vector3.New()
        pos.x = bag.x + bag.width/2
        pos.y = bag.y + bag.height/2
        local x,y = ObjUtil.TransformPoint(content2,pos,gameObject)
        logicMgr.ItemFlyMgr.SetBagPos(x,y)
    end
end
function UIFuncMenu.UpdateState()
    this.openAddHeight1 = 0;
    local itemNum = #tab_1;
    content1.itemRenderer = this.RenderListItem1;
    if itemNum == 0 then
        imgContent1.visible = false;
    else
        imgContent1.visible = true;
        this.RenderListItem(tab_1[1], btnItem);
        content1.numItems = itemNum - 1;
        if this.isOpen == 2 then
            this.onOpenList(1);
        else
            this.onCloseList(1);
        end
        funcList1.visible = this.getListNum(1) > (this.leftNum - 1);
        if itemNum <= 8 then
            content1.scrollPane.touchEffect = false;
        else
            content1.scrollPane.touchEffect = true;
        end
    end
end

--- 获取最近的限时运营活动
--- @param list table 所有限时活动列表
--- @return table 活动数据
function UIFuncMenu.getLatestLimitActData(list)
    for _, cfgData in pairs(list) do
        local __, serverData = logicMgr.ActiveManager.getActiveData(tonumber(cfgData.f_JumpCo))
        if serverData and serverData.isShow then
            mActServerData[tostring(cfgData.f_ID)] = serverData
            return cfgData
        end
    end
    return nil
end

function UIFuncMenu.RenderListItem1(idx, obj)
    local v = tab_1[idx + 2];
    this.RenderListItem(v, obj);
end

function UIFuncMenu.RenderListItem2(idx, obj)
    local v = tab_2[idx + 1];
    this.RenderListItem(v, obj);
end

--function UIFuncMenu.RenderListItem3(idx, obj)
--    local v = tab_3[idx + 1];
--    this.RenderListItem(v, obj);
--end

function UIFuncMenu.RenderListItem(v, obj)
    local icon = v.f_Icon;
    -- local uiName = v.f_OpenUI;
    local name = getLanguage(v.f_Desc);

    local itemData = {};
    itemData["icon"] = icon;
    itemData["name"] = name;
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.FuncSlotItem);
    if ctrlItem ~= nil then
        --初始装备信息
        ctrlItem.initData(obj, itemData);
    end
    local rbCtrl = obj:GetController("RankBattle")
    local timeTxt = obj:GetChild("Text_OpenTime")
    local spineCtrl = obj:GetController("spine")
    rbCtrl.selectedIndex = 0
    if v.f_ID ==1 or v.f_ID == 30001 or v.f_ID==20001 or v.f_ID == 11 then
        spineCtrl.selectedIndex = 1
    else
        spineCtrl.selectedIndex = 0
    end
    if v.f_ID == 10001 and logicMgr.CostManager.GetServerRebateType() == 2 then
        spineCtrl.selectedIndex = 1;
        --local redController = obj:GetChild("UIRedDots"):GetController("c1");
        --redController.selectedIndex = 2;
    end
    if v.f_ID == 7 then
        this.openAddHeight1 = this.openAddHeight1 + 10;
    elseif v.f_ID == 8 then --七日榜
        rbCtrl.selectedIndex = 1
        this.openAddHeight1 = this.openAddHeight1 + 10;
        this.time_txt_content = HelperFunc.GetTimeStr2(logicMgr.NoticeManager.GetCD())
        timeTxt.text = this.time_txt_content
        utimer.remove(this.timeIdList[v.f_ID])
        this.timeIdList[v.f_ID] = utimer.timeCall(function()
            local cd = logicMgr.NoticeManager.GetCD()
            local text_content = HelperFunc.GetTimeStr2(cd)
            if this.time_txt_content ~= text_content then
                timeTxt.text = text_content
                this.time_txt_content = text_content
            end

            if cd <= 0 then
                this.initData()
            end
        end, 1000, -1);
    elseif v.f_ID == 11 then --七日排行榜
        --rbCtrl.selectedIndex = 1
        --this.openAddHeight1 = this.openAddHeight1 + 10;
        --utimer.remove(this.timeIdList[v.f_ID])
        --this._day, this.time_txt_content11 = logicMgr.NoticeManager.getSevenRankDay()
        --if not logicMgr.NoticeManager.checkSevenRankIsRanking(this._day) then
        --    timeTxt.text = getLanguage("RaceRankDes4");
        --    if this.time_txt_content11 then
        --        this.timeIdList[v.f_ID] = utimer.timeCall(function()
        --            this.initData();
        --        end, this.time_txt_content11 - HelperFunc.GetServerTime());
        --    end
        --else
        --    timeTxt.text = HelperFunc.GetTimeStr(this.time_txt_content11 - HelperFunc.GetServerTime());
        --    this.timeIdList[v.f_ID] = utimer.timeCall(function()
        --        if this.time_txt_content11 - HelperFunc.GetServerTime() <= 0 then
        --            timeTxt.text = getLanguage("RaceRankDes4");
        --            this.initData();
        --            FireEvent(Config.EventType.Fresh_SevenRankRedDot,this.update);
        --        else
        --            timeTxt.text = HelperFunc.GetTimeStr(this.time_txt_content11 - HelperFunc.GetServerTime());
        --        end
        --    end, 1000, -1);
        --end
    elseif v.f_ID == 13 then --七日排行榜
        obj:GetChild("title").text = getLanguage(logicMgr.NoticeManager.getSevenRankPlusName());
        rbCtrl.selectedIndex = 1
        this.openAddHeight1 = this.openAddHeight1 + 10;
        utimer.remove(this.timeIdList[v.f_ID])
        this._plusDay, this.plus_time_txt_content11 = logicMgr.NoticeManager.getSevenRankPlusDay()
        timeTxt.text = HelperFunc.GetTimeStr(this.plus_time_txt_content11 - HelperFunc.GetServerTime());
        this.timeIdList[v.f_ID] = utimer.timeCall(function()
            if this.plus_time_txt_content11 - HelperFunc.GetServerTime() <= 0 then
                timeTxt.text = getLanguage("RaceRankDes4");
                this.initData();
                FireEvent(Config.EventType.Fresh_SevenRankRedDot, this.update);
            else
                timeTxt.text = HelperFunc.GetTimeStr(this.plus_time_txt_content11 - HelperFunc.GetServerTime());
            end
        end, 1000, -1);
    end
    if v.f_ID == logicMgr.CostManager._FLASHGIFTID then
        rbCtrl.selectedIndex = 1;
        this.openAddHeight1 = this.openAddHeight1 + 10;
        timeTxt.text = timeToTimeFormat(logicMgr.FlashGiftManager.getCountTime());
        utimer.remove(this.timeIdList[v.f_ID])
        this.timeIdList[v.f_ID] = utimer.timeCall(function()
            timeTxt.text = timeToTimeFormat(logicMgr.FlashGiftManager.getCountTime());
        end, 1000, -1);
    end

    -- 限时活动
    if v.f_Group == 1 then
        this.showLimitActLeftTime(v.f_ID, tonumber(v.f_JumpCo), rbCtrl, timeTxt)
    end

    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIFuncMenu, v.f_FuncOpen, obj.name, obj);
    table.insert(this.itemObjList, { v.f_FuncOpen, obj });
    obj.onClick:Clear();
    obj.onClick:Add(function()
        if isOpenUI(PanelNames.UIHeadMenu) and CtrlManager.GetCtrl(PanelNames.UIHeadMenu).isHaveShowMask() then
            return ;
        end
        this.onClickFuncItem(v, obj)
    end);

    local redType = v.f_HintType;
    if redType ~= nil and redType > 0 then
        if (v.f_OpenUI == "UICost" and v.f_JumpCo ~= "") or v.f_Group == 1 then
            UIRedDots.addViewListen(PanelNames.UIFuncMenu, gameObject.name .. v.f_ID, obj, redType, tonumber(v.f_JumpCo));
        else
            UIRedDots.addViewListen(PanelNames.UIFuncMenu, gameObject.name .. v.f_ID, obj, redType, v.id);
        end
        if redType==Config.RedDotsType.Bag then
            UIRedDots.addViewListen(PanelNames.UIFuncMenu, gameObject.name .. v.f_ID, obj, Config.RedDotsType.ActivatePartner)
        end
        --if redType==Config.RedDotsType.Task then
        --    UIRedDots.addViewListen(PanelNames.UIFuncMenu, gameObject.name .. v.f_ID, obj, Config.RedDotsType.UnlockDesc)
        --end
    end
end

--- 限时活动时间展示
--- @param id number 功能id
--- @param actId number 活动id
--- @param uiTimeCtrl GComponent 控制器
--- @param uiTxtTime GComponent 文本
function UIFuncMenu.showLimitActLeftTime(id, actId, uiTimeCtrl, uiTxtTime)
    if uiTimeCtrl == nil or uiTxtTime == nil then
        return
    end

    this.openAddHeight1 = this.openAddHeight1 + 10
    uiTimeCtrl.selectedIndex = 1
    uiTxtTime.text = HelperFunc.GetSurpTimeStr(this.getLimitActLeftTime(actId))

    utimer.remove(this.timeIdList[id])
    this.timeIdList[id] = utimer.timeCall(function()
        local leftTime = this.getLimitActLeftTime(actId)
        uiTxtTime.text = HelperFunc.GetSurpTimeStr(leftTime)
        if leftTime <= 0 then
            this.initData()
        end
    end, 1000, -1)
end

--- 获取限时活动剩余时间
--- @param actId number 活动id
--- @return number 剩余时间
function UIFuncMenu.getLimitActLeftTime(actId)
    local serverData = mActServerData[tostring(actId)]
    if serverData == nil then
        local _, data = logicMgr.ActiveManager.getActiveData(actId)
        if data then
            mActServerData[tostring(actId)] = data
            serverData = data
        end
    end

    if serverData ~= nil then
        local currTime = HelperFunc.GetServerTime()
        return serverData.endTime - currTime
    end

    return 0
end

--function UIFuncMenu.onClickClose()
--    closeUI(PanelNames.UIFuncMenu);
--    UICity.onOpenFunc();
--    UIBattle.onOpenFunc();
--end

function UIFuncMenu.onClickFuncItem(funcMenumData, obj)
    if logicMgr.FuncOpenManager.isOpen(funcMenumData.f_FuncOpen, true, obj) then
        if funcMenumData.f_ID == 2 then
            --任务
            AudioManager.Play(1026)
        elseif funcMenumData.f_ID == 4 then
            --背包
            AudioManager.Play(1014)
        else
            AudioManager.Play(1004)
        end
        local uiName = ConverStringToEnum(PanelNames, funcMenumData.f_OpenUI);
        log(uiName);
        if uiName ~= nil then
            if funcMenumData.f_OpenUI == "UISystemBillBoard" then
                logicMgr.UIShowManager.ShowBillboardRank(tonumber(funcMenumData.f_JumpCo),function()
                    if isOpenUI(PanelResNames.UICity) then
                        UIBottom.onClickTown();
                    else
                        UIBottom.onClickBattle();
                    end
                        logicMgr.UIShowManager.ShowBillboardRank()

                    end)
            elseif funcMenumData.f_OpenUI == "UIGuildBattlePlus" then
                unionWarPlusHandler.reqGuildBattleInfo(1, EnumConst.GuildBattlePlusType.Championship);
            elseif funcMenumData.f_JumpCo and funcMenumData.f_JumpCo ~= "" then
                showUI(uiName, nil, funcMenumData.f_JumpCo);
            else
                showUI(uiName, nil, funcMenumData.id);
            end
        end
        if funcMenumData.f_ID == 15 then    --社区(进社区前如果是当前是在主城中，先引导至冒险界面，否则回来的时候会错乱)
            utimer.delayCall(function()
                if UIBottom.pageController.selectedIndex == 0 then
                    UIBottom.onClickBattle()
                end
            end, 1000)
            LoginSDK.GetInstance():GoToTaptapMoment();
        end
    end
end

--- 点击活动按钮
function UIFuncMenu.onClickActivity()
    AudioManager.Play(2037)
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Activity, true, this.btnActivity) then
        showUI(PanelNames.UIActivity)
    end
end

function UIFuncMenu.onOpenList(type)
    local height = 0;
    local num = 0;
    if type == 1 then
        num = this.getListNum(1);
        if num > 0 then
            if num > 7 then
                num = 7;
            end
            local h = content1:GetChildAt(0).height;
            height = num * h + num * content1.lineGap;
        end
        local TweenSC = content1:TweenResize(Vector2(content1.width, height + this.openAddHeight1), 0.3);
        TweenSC:OnComplete(function()
            funcList1.rotation = 180;
            funcList1:GetChild("UIRedDots").visible = false;

        end)
        showRedDots1.selectedIndex = 0;
    elseif type == 2 then
        num = this.getListNum(2);
        if num > 0 then
            local h = content2:GetChildAt(0).height;
            height = num * h + num * content2.lineGap;
        end
        local TweenSC = content2:TweenResize(Vector2(content2.width, height), 0.3);
        TweenSC:OnComplete(function()
            funcList2.rotation = 180;
            funcList2:GetChild("UIRedDots").visible = false;
        end)
        showRedDots2.selectedIndex = 0;
    end

end

function UIFuncMenu.onCloseList(type)
    --content1.itemRenderer = this.RenderListItem1;
    --content1.numItems = count1;
    --content2.itemRenderer = this.RenderListItem2;
    --content2.numItems = count2;
    local height = 0;
    local num = 0;
    if type == 1 then
        num = this.getListNum(1);
        local h = 0;
        if num > 0 then
            h = content1:GetChildAt(0).height;
        end
        if num < this.leftNum then
            height = num * h + num * content1.lineGap;
        else
            num = this.leftNum - 1;
            height = num * h + num * content1.lineGap;
        end
        local TweenSC = content1:TweenResize(Vector2(content1.width, height), 0.3);
        TweenSC:OnComplete(function()
            if funcList1 == nil then
                return ;
            end
            funcList1.rotation = 0;
            funcList1:GetChild("UIRedDots").visible = true;

        end)
        showRedDots1.selectedIndex = 1;
    elseif type == 2 then
        num = this.getListNum(2);
        local h = content2:GetChildAt(0).height;
        if num < this.rightNum then
            height = #count2 * h + #count2 * content2.lineGap;
        else
            height = this.rightNum * h + this.rightNum * content2.lineGap;
        end
        local TweenSC = content2:TweenResize(Vector2(content2.width, height), 0.3);
        TweenSC:OnComplete(function()
            if funcList2 == nil then
                return ;
            end
            funcList2.rotation = 0;
            funcList2:GetChild("UIRedDots").visible = true;
        end)
        showRedDots2.selectedIndex = 1;
    end

end

function UIFuncMenu.getListNum(type)
    local data = {};
    local lists = {};
    if type == 1 then
        data = count1;
        lists = content1;
    elseif type == 2 then
        data = count2;
        lists = content2;
    end
    local num = 0;
    for i = 1, lists.numChildren do
        if lists:GetChildAt(i - 1).visible then
            num = num + 1;
        end
    end
    return num;
end
function UIFuncMenu.FlyBagTransition()
    if not hasInit then return end
    if not content2  then
        return
    end
    local target = content2:GetChildAt(2);
    local trans = target:GetTransition("t0")
    trans:Play()
end

--- desc: 切换界面底部标签
--- @private
function UIFuncMenu.onEventBottomChangePage(page)
    this.leftNum = page == EnumConst.BottomPageType.city and 100 or 3
    this.initData()
end

return UIFuncMenu;